package cn.yunhe.servlet;

import cn.yunhe.biz.AdminService;
import cn.yunhe.pojo.Admin;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/AdminLoginServlet")
public class AdminLoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        Admin a=new Admin(username,password);
        AdminService adminService = new AdminService();
        Admin admin =adminService.loginAdmin(a);
        if (admin!=null){
            HttpSession session = request.getSession();
            session.setAttribute("admin",admin);
            request.getRequestDispatcher("ToMainServlet").forward(request,response);
        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }
}
